package com.example.bookmanager;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.xuyang.fragment.ListFragment;
import com.xuyang.fragment.SelectFragment;
import com.xuyang.fragment.UserFragment;

public class MainActivity extends Activity {
	private ImageView imgSelect,imgList,imgUser;
	private TextView txtSelect,txtList,txtUser;
	private LinearLayout layoutSelect,layoutList,layoutUser;
	private LinearLayout layoutFragment;
	private FragmentManager manager=getFragmentManager();
	
	private Fragment mSelectFragment,mListFragment;
	private UserFragment mUserFragment;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//初始化时控件的获取
		init();
		// 设置点击事件
		setOnClick();
		//刚进入时默认进入首页
		new OnMenuClick().onClick(layoutSelect);
	}
	private void init() {
		imgSelect=(ImageView) findViewById(R.id.img_select);
		imgList=(ImageView) findViewById(R.id.img_list);
		imgUser=(ImageView) findViewById(R.id.img_user);
		txtSelect=(TextView) findViewById(R.id.txt_select);
		txtList=(TextView) findViewById(R.id.txt_list);
		txtUser=(TextView) findViewById(R.id.txt_user);
		layoutSelect=(LinearLayout) findViewById(R.id.layout_select);
		layoutList=(LinearLayout) findViewById(R.id.layout_list);
		layoutUser=(LinearLayout) findViewById(R.id.layout_user);
		layoutFragment=(LinearLayout) findViewById(R.id.layout_fragment);
		
		mUserFragment=new UserFragment();
		mListFragment=new ListFragment();
		mSelectFragment=new SelectFragment();
	}
	private void setOnClick() {
		layoutSelect.setOnClickListener(new OnMenuClick());
		layoutList.setOnClickListener(new OnMenuClick());
		layoutUser.setOnClickListener(new OnMenuClick());
		
	}
	//双击退出按钮
	private long endTime;
    @Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		 if (keyCode == KeyEvent.KEYCODE_BACK) {
	            exit();
	            return false;
	        }
	        return false;
	    }

	    public void exit() {
	        if ((System.currentTimeMillis() - endTime) > 3000) {
	            Toast.makeText(getApplicationContext(), "再次单击退出程序",
	                    Toast.LENGTH_SHORT).show();
	            endTime = System.currentTimeMillis();
	        } else {
	            finish();
	            System.exit(0);
	        }
	    }
    
	 class OnMenuClick implements OnClickListener{
		@Override
		public void onClick(View v) {
			FragmentTransaction ft=manager.beginTransaction();
			switch (v.getId()) {
			case R.id.layout_select:
				ft.replace(R.id.layout_fragment, mSelectFragment).commit();
				imgSelect.setImageResource(R.drawable.select_clicked);
				txtSelect.setTextColor(getResources().getColor(R.color.menu_clicked));
				imgList.setImageResource(R.drawable.browse_noclick);
				txtList.setTextColor(getResources().getColor(R.color.menu_default));
				imgUser.setImageResource(R.drawable.user_noclick);
				txtUser.setTextColor(getResources().getColor(R.color.menu_default));
				break;
			case R.id.layout_list:
				ft.replace(R.id.layout_fragment,mListFragment).commit();
				imgSelect.setImageResource(R.drawable.select_noclick);
				txtSelect.setTextColor(getResources().getColor(R.color.menu_default));
				imgList.setImageResource(R.drawable.browse_clicked);
				txtList.setTextColor(getResources().getColor(R.color.menu_clicked));
				imgUser.setImageResource(R.drawable.user_noclick);
				txtUser.setTextColor(getResources().getColor(R.color.menu_default));
				break;
			case R.id.layout_user:
				ft.replace(R.id.layout_fragment,mUserFragment).commit();
				imgSelect.setImageResource(R.drawable.select_noclick);
				txtSelect.setTextColor(getResources().getColor(R.color.menu_default));
				imgList.setImageResource(R.drawable.browse_noclick);
				txtList.setTextColor(getResources().getColor(R.color.menu_default));
				imgUser.setImageResource(R.drawable.user_clicked);
				txtUser.setTextColor(getResources().getColor(R.color.menu_clicked));
				break;
			default:
				break;
			}
		}
	}
}
